<?php 
define('IN_QY',true);
session_start();
require("include/common.inc.php");
if(!$_COOKIE['userid']){
    echo "<script type='text/javascript'>location.href='/wap/mlogin.php';</script>";
    exit;
}

	$sqlmo = "update tbl_taorder set is_pay=2 where is_pay=0 AND uid=".$_COOKIE['userid'];
    $res=mysql_query($sqlmo);
	
	
//未支付订单判断
$sql = "select * from tbl_taocart where userid='".$_COOKIE['userid']."' and isorder=0 and is_tax=1";
$query = mysql_query($sql);
$rowcart= mysql_fetch_array($query);
if (!$rowcart) {
  echo "<script type='text/javascript'>alert('您的海淘购物车是空的!');history.go(-1);</script>";
  exit;
}
//订单号
$order_id = $_COOKIE['userid']."a".date('YmdHis',time());

$proarr1 = array(9,1,10,11,16,18,17,12,14,19);
$proarr2 = array(3,2,13,22,15,23,20,21);
$proarr3 = array(27,4,7,24,6,25,8,5,28,29,30);
$proarr4 = array(26,31);
$numarr = array();
$weiarr = array();
$priarr = array();
$tempyb = "";
$sql = "select t1.*,t2.typeid,t2.ggnum,t2.entitle,t2.encode from tbl_taocart t1 inner join tbl_taospp t2 on t1.goodsid=t2.id  where t1.userid='".$_COOKIE['userid']."' and t1.isorder=0 and t1.is_tax=1 group by t1.id";
$query = mysql_query($sql);
    
while($row=mysql_fetch_array($query)){

    $totpri = $row['goodsnum']*$row['goodspri'];//总价
    if ($row['entitle']!=9) {
      $priarr[] = $totpri;
    }
    
    $weight = "";//重量
    if ($row['typeid']!=2 ) {
      if ($row['buyunit']==1) {
        $weight = $row['goodsnum']*$row['encode'];
      }elseif ($row['buyunit']==2) {
        $weight = $row['goodsnum']*$row['ggnum']*$row['encode'];
      }
    }  
    $weiarr[] = $weight;

    if ($row['typeid']==2 && $row['entitle']!=9 && $row['entitle']!=18) {
      $numarr[] = $row['goodsnum'];
    }
    if ($row['entitle']==18) {
      $numarr1[] = $row['goodsnum'];//椰子
    }
    $tempyb = $row['youb'];
	//清空购物车原有的
	

	
	
    //写订单表
    $sql = "insert into tbl_taorder values (
	0,
	'".$_COOKIE['userid']."',
	'".$row['goodsid']."',
	'".$order_id."',
	'".$row['goodspri']."',
	
	'".$row['goodsnum']."',
	'".$row['buyunit']."',
	'".$row['ggnum']."',
	0,
	'".time()."',
	0,
	0,
	0,
	0,
	0,
	'".$totpri."',
	0,
	0,
	0,
	0,
	'".$row['buyunit']."',
	0)";
    mysql_query($sql);
}
//运费
$delinum = array_sum($numarr);
$delinum1 = array_sum($numarr1);
$delifee = $delinum*40;
$delifee1 = $delinum1*20;
$wei = array_sum($weiarr);
$weis = ceil($wei);
$pri = array_sum($priarr);
$express = "";

  if($wei==0) {
      $express = 0;
  }elseif($wei<=1 && $wei>0) {
      if (in_array($tempyb, $proarr1)) {
        $express = 12;
      }elseif (in_array($tempyb, $proarr2)) {
        $express = 15;
      }elseif (in_array($tempyb, $proarr3)) {
        $express = 20;
      }elseif (in_array($tempyb, $proarr4)) {
        $express = 25;
      }elseif (in_array($tempyb, $proarr5)) {
        $express = 30;
      }
   }else{
      if (in_array($tempyb, $proarr1)) {
        $express = 12+($weis-1)*5;
      }elseif (in_array($tempyb, $proarr2)) {
        $express = 15+($weis-1)*8;
      }elseif (in_array($tempyb, $proarr3)) {
        $express = 20+($weis-1)*10;
      }elseif (in_array($tempyb, $proarr4)) {
        $express = 25+($weis-1)*20;
      }elseif (in_array($tempyb, $proarr5)) {
        $express = 30+($weis-1)*18;
      }
   }


$totfee = $delifee+$delifee1+$express;
//taocart
$sql = "select max(id) AS oid from tbl_taorder where orderid='".$order_id."'";
$query = mysql_query($sql);
$res = mysql_fetch_array($query);
$sql = "update tbl_taorder set expresstri='".$totfee."' where id='".$res['oid']."'";
mysql_query($sql);
//改购物车状态




//改购物车状态

  //$sql = "update tbl_taocart set isorder=1 where userid='".$_COOKIE['userid']."' and isorder=0 and is_tax=1";
//  $resy=mysql_query($sql);


setcookie("dindanid",$order_id,time()+(3600*8*1*1),"/");   




    echo "<script type='text/javascript'>location.href='htdjxiadan.php'</script>";
    exit;

